/**
 * Created by luckyjing on 15/12/22.
 */
var https = require('https');
var AppID = "wxd8f65d8da47e5173";
var AppSecret = "1fbc5dd4d4d0003314a279419981e739";
var userInfoArray = [];
var id = 1;
exports.userInfoArray = userInfoArray;
exports.find = function (success) {
    var headers = {};
    var options = {
        host: 'localhost',
        port: 8080,
        method: 'get',
        path: '/user/toLogin.do',
        headers: headers
    };
    var req = https.request(options, function (res) {
        res.setEncoding('utf8');
        res.on('data', function (data) {
            console.log(data);
            success(data);
        });
    });
    req.on('error', function (e) {
        console.log("错误" + e);
    });
    req.end();
};
exports.getAccessToken = function (code, success) {
    var url = ' https://api.weixin.qq.com/sns/oauth2/access_token?appid=' + AppID + '&secret=' + AppSecret + '&code=' + code + '&grant_type=authorization_code';
    console.log('获取accessTnioken的url为:'+url);
    var req = https.get(url, function (res) {
        res.setEncoding('utf8');
        res.on('data', function (data) {
            console.log('getToken的返回值:' + data);
            success(JSON.parse(data));
        });
    });
    req.on('error', function (e) {
        console.log('错误:' + e);
    });
    req.end();
};
exports.getUser = function (options, success) {
    var req = https.get('https://api.weixin.qq.com/sns/userinfo?access_token=' + options.access_token + '&openid=' + options.openid + '&lang=zh_CN', function (res) {
        res.setEncoding('utf8');
        res.on('data', function (data) {
            console.log('getUserInfo的返回值' + data);
            userInfoArray.push({
                id: id,
                data: data
            });
            success(JSON.parse(data));
            id++;
        });
    });
    req.on('error', function (e) {
        console.log('错误:' + e);
    });
    req.end();
};
